class Solution {
public:
    bool check(vector<int>& postorder,int start,int end){
        if(start >= end){
            return true;
        }
        int i = start;
        int mid = mid;
        while(i < end && postorder[i] < postorder[end]){
            i++;
        }
        mid = i;
        while(i < end && postorder[i] > postorder[end]){
            i++;
        }
        
        return i == end && check(postorder,start,mid-1) && check(postorder,mid,end-1);
    }
                                        
    bool verifyPostorder(vector<int>& postorder) {
        int n = postorder.size();
        
        return check(postorder,0,n-1);
    }
};
